package com.lzx.tease.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzx.common.utils.R;
import com.lzx.tease.entity.Recommend;
import com.lzx.tease.mapper.RecommendMapper;
import com.lzx.tease.service.RecommendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lzx
 * @since 2022-12-20
 */
@Service
public class RecommendServiceImpl extends ServiceImpl<RecommendMapper, Recommend> implements RecommendService {
    @Resource
    private RecommendMapper recommendMapper;

    //保存用户的建议
    @Override
    public R saveRecommend(Recommend recommend) {
        QueryWrapper<Recommend> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", recommend.getUserId());
        Recommend vo = recommendMapper.selectOne(wrapper);
        if (vo != null) {
            return R.ok();
        } else {
            recommend.setCreateTime(new Date());
            recommendMapper.insert(recommend);
            return R.ok().message("提交成功");
        }
    }

    //查询当前用户是否已经提交建议
    @Override
    public R queryRecommendByUserId(int userId) {
        QueryWrapper<Recommend> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        return R.ok().data("recommend", recommendMapper.selectOne(wrapper));
    }
}
